package cn.tj212.xiaoyin.core.course.service.impl;

import cn.tj212.xiaoyin.common.page.TailPage;
import cn.tj212.xiaoyin.common.storage.QiniuStorage;
import cn.tj212.xiaoyin.core.consts.CourseEnum;
import cn.tj212.xiaoyin.core.course.dao.CourseDao;
import cn.tj212.xiaoyin.core.course.domain.Course;
import cn.tj212.xiaoyin.core.course.domain.CourseQueryDto;
import cn.tj212.xiaoyin.core.course.service.ICourseService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private CourseDao entityDao;

    private void prepareCoursePicture(Course course){
        if (null!=course&&StringUtils.isNotEmpty(course.getPicture()))
            course.setPicture(QiniuStorage.getUrl(course.getPicture()));
    }

    @Override
    public Course getById(Long courseId) {
       Course course=entityDao.getById(courseId);
       prepareCoursePicture(course);
       return course;
    }

    @Override
    public List<Course> queryList(CourseQueryDto queryEntity) {
        if (null==queryEntity.getOnsale())
            queryEntity.setOnsale(CourseEnum.ONSALE.value());
        return entityDao.queryList(queryEntity);
    }

    @Override
    public TailPage<Course> queryPage(Course queryEntity, TailPage<Course> page) {
        Integer itemsTotalCount=entityDao.getTotalItemsCount(queryEntity);
        List<Course> items=entityDao.queryPage(queryEntity,page);
        if (CollectionUtils.isNotEmpty(items)){
            for (Course item:items
                 ) {
                prepareCoursePicture(item);
            }
        }
        page.setItemsTotalCount(itemsTotalCount);
        page.setItems(items);
        return page;
    }

    @Override
    public void createSelectivity(Course entity) {
        entityDao.createSelectivity(entity);
    }

    @Override
    public void updateSelectivity(Course entity) {
        entityDao.updateSelectivity(entity);
    }

    @Override
    public void delete(Course entity) {
        entityDao.delete(entity);
    }

    @Override
    public void deleteLogic(Course entity) {
        entityDao.deleteLogic(entity);
    }
}
